import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
let audio;
let timeout
const store = new Vuex.Store({
	state: {
		//公共的变量，这里的变量不能随便修改，只能通过触发mutations的方法才能改变
		list: [],
		playStatus: false, //播放与暂停标识
		currentPlayIndex: 0, //当前歌曲标识
		durationTime: 0, //音频总时长
		currentTime: 0, //音频播放时刻
		playId: "", //当前歌曲id
		playType: 0, //0随机播放 1顺序播放
	},
	mutations: {
		//相当于同步的操作
		setList(state, n) {
			state.list = n
		},
		setSpeed(state, n) {
			audio.playbackRate = n
		},
		setPlayType(state, n) {
			state.playType = n
		},
		// 销毁事件
		destruction() {
			audio.offPlay()
			// audio.offPause()
			// audio.offStop()
			audio.offEnded()
			audio.offError()
		},
		// 播放
		audioPlay(state) {
			let curIndex = state.currentPlayIndex
			audio.src = state.list[curIndex].yp_file_text
			// audio.obeyMuteSwitch = false
			state.playId = state.list[curIndex].id
			audio.currentTime = state.currentTime
			console.log(123);
			audio.play()
		},
		// 跳转
		audioSeek(state, pos) {
			audio.seek(pos)
		},
		// 暂停
		audioPause() {
			audio.pause()
		},
		// 停止
		audioStop() {
			audio.stop()
		},
		// 改变播放与暂停标识
		changePlayStatus(state, Boolean) {
			state.playStatus = Boolean
		},
		// 改变播放标识
		changePlayIndex(state, index) {
			state.currentPlayIndex = index
			console.log(state.currentPlayIndex);
		},
		getDurationTime(state, time) {
			state.durationTime = time
		},
		// 改变当前时间（暂停时）
		changeCurrentTime(state, time) {
			state.currentTime = time
		},
	},
	getters: {
		// 音频name
		audioName(state) {
			let curIndex = state.currentPlayIndex
			if(state.list.length){
				return state.list[curIndex].title
			}
		},
		//音频路径
		audioSrc(state){
			let curIndex = state.currentPlayIndex
			if(state.list.length){
				return state.list[curIndex].yp_file_text
			}
		}
	},
	actions: {
		//相当于异步的操作,不能直接改变state的值，只能通过触发mutations的方法才能改变
		// 初始化
		inittext({
			state,
			commit,
			dispatch
		}) {
			if (audio) {
				return
			}
			console.log("audio实例化了！")
			audio = uni.getBackgroundAudioManager(); //实例化audio对象
			console.log("audio实例化了12！")
			// 监听
			// audio.onCanplay(() => {
			// 	console.log("监听到了9999999999");
			// 	const intervalID = setInterval(() => {
			// 		if (audio.duration !== 0) {
			// 			clearInterval(intervalID);
			// 			console.log(audio.src,"播放地址");
			// 			console.log(audio.duration,"播放时长");
			// 			// commit('getDurationTime', audio.duration)
			// 			commit("getDurationTime",state.list[state.currentPlayIndex].duration)
			// 		}
			// 	}, 500);
			// })
			// 暂停事件
			audio.onPause(() => {
				commit('changePlayStatus', false)
				console.log("暂停播放")
			})
			// 播放结束事件
			audio.onEnded(() => {
				commit('changePlayStatus', false)
				commit('changeCurrentTime', 0)
				state.playType == 0 ? dispatch('PreOrStopRandomPlay') : dispatch('PreOrStopNext')
				console.log("播放结束")
				// audio.destroy()
			})
			// 播放错误事件
			audio.onError((res) => {
				commit('changePlayStatus', false)
				console.log(res.errMsg);
				console.log(res.errCode);
				// audio.destroy()
			})
			//  更新
			audio.onTimeUpdate(() => {
				console.log(audio.currentTime,"更新了");
				commit('changeCurrentTime', audio.currentTime)
			})
		},
		// 播放与暂停
		playOrPause({
			state,
			commit
		}) {
			if (!state.playStatus) {
				commit("audioPlay")
			} else {
				commit("audioPause")
			}
		},
		// 点击歌曲播放歌曲
		PreOrNext({
			state,
			commit
		}, type) {
			commit("audioStop")
			let curIndex = state.currentPlayIndex
			commit("changePlayIndex", curIndex)
			commit("getDurationTime",state.list[curIndex].duration)
			commit("audioPlay")
		},
		PreOrStopNext({
			state,
			commit
		}, type) {
			commit("audioStop")
			let curIndex = state.currentPlayIndex
			let lastIndex = state.list.length - 1
			curIndex === lastIndex ? commit("changePlayIndex", 0) : commit("changePlayIndex", curIndex + 1)
			commit("audioPlay")
		},
		PreOrStopRandomPlay({
			state,
			commit
		}) {
			commit("audioStop")
			let curIndex = state.currentPlayIndex
			curIndex = Math.round(Math.random() * state.list.length)
			console.log(curIndex)
			commit("changePlayIndex", curIndex)
			commit("audioPlay")
		},
		// 切歌
		// PreOrNext({
		// 	state,
		// 	commit
		// }, type) {
		// 	commit("audioStop")
		// 	let curIndex = state.currentPlayIndex
		// 	let lastIndex = musics.length - 1
		// 	switch (type) {
		// 		case 'prev':
		// 			curIndex === 0 ? commit("changePlayIndex", lastIndex) : commit("changePlayIndex", curIndex -
		// 				1)
		// 			break;
		// 		case 'next':
		// 			curIndex === lastIndex ? commit("changePlayIndex", 0) : commit("changePlayIndex", curIndex +
		// 				1)
		// 			break;
		// 		default:
		// 			break;
		// 	}
		// 	commit("audioPlay")
		// },
		sliderToPlay({
			state,
			commit
		}, {
			detail: {
				value: position
			}
		}) {
			let time = position
			console.log(time);
			commit("audioSeek", position)
			if (!state.playStatus) {
				clearTimeout(timeout)
				timeout = setTimeout(() => commit('changeCurrentTime', time), 200)
			}
		}
	},
})
export default store